<?php
/**
 * User: ppjun378
 * Data: 2019/01/30
 * Time: 17:00
 */
namespace App\Service;

use App\Model\Pictures;
use App\Model\PicturesCategorys;

class PicturesServicew
{
    /**
     * 获取图片数据
     * @parama $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function dataList($request, $pic_type = 1)
    {
        $survey_id = $request->input('survey_id', '');

        $fields = [
            'survey_id', //查勘表编号
            'img_url', // 图片地址
            'img_type', // 图片类型
            'img_alt', // 图片描述
            'name as picture_category_name', //图片名称
        ];
        $data = PicturesCategorys::select($fields)
            ->leftJoin('pictures', function ($join) use ($survey_id) {
                $join->on('pictures.img_type', '=', 'pictures_categorys.id')
                    ->where('survey_id', $survey_id);
            })

            ->where('pic_type', $pic_type)
            ->orderBy('pictures_categorys.id', 'asc')
            ->get();

        $list['code'] = 0;
        $list['msg'] = '查询成功';
        $list['count'] = count($data);
        $data = empty($data) ? array() : $data->toArray();

        $list['data'] = $data;
        return $list;
    }

    /**
     * 添加/更新
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function store($request, $pic_type = 1)
    {

    }

    /**
     * 删除
     * @param $request
     * @return bool\int
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function destory($request)
    {
        $id = $request->input('id', '');
        $res = Pictures::destory($id);
        if ($res > 0) {
            return $res;
        } else {
            return $false;
        }
    }

    /**
     * 恢复删除
     * @param $request
     * @return $mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function restore($request)
    {
        $id = $request->input('id', '');
        $res = Pictures::where('id', $id)->restore();
        return $res;
    }

}
